<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>16-向量方法实现物体运动</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
    }
  </style>
</head>

<body>
  <script type="module">
    import * as THREE from "three";
    import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
    import {
      scene,
      camera,
      renderer
    } from "../utils/three-base";
    import { Timer } from "three/addons/misc/Timer";
    import { renderArrowHelper } from "../utils/utils";
    // 渲染出三维辅助线
    renderArrowHelper(30);

    // 设置摄像机视角
    camera.position.set(0.5, 2.6, 4.2);
    camera.lookAt(0, 0, 0);

    // 添加网格地板
    const size = 100;
    const divisions = 100;

    const gridHelper = new THREE.GridHelper(size, divisions, 0x2E8B57, 0x2E8B57);
    scene.add(gridHelper);

    /*
      a.add(b) // 存储在向量a中
      (1, 2, 3) + (1, 1, 1) = (2, 3, 4)

      a.multiplyScalar(b) // 将当前向量的每个分量都乘以一个标量
      (1, 2, 3) * 0.5 = (0.5, 1, 1.5) 

      a.addScaledVector() // 将一个向量和一个标量的乘积加到当前向量上。就是multiplyScalar和add结合体
    */



    // 添加一个代表人物的物体
    const player = new THREE.Mesh(
      new THREE.CylinderGeometry(0.2, 0.2, 1, 32),
      new THREE.MeshBasicMaterial({
        color: 0xffff00
      })
    );
    player.position.set(0, 0.5, 0);
    scene.add(player);

    // 创建人物移动方向枚举
    const keyEnum = {
      W: false,
      S: false
    }

    window.addEventListener("keydown", (event) => {
      const keyCode = event.key.toUpperCase();
      if(keyEnum.hasOwnProperty(keyCode)) {
        keyEnum[keyCode] = true;
      }
    });

    window.addEventListener("keyup", (event) => {
      const keyCode = event.key.toUpperCase();
      if (keyEnum.hasOwnProperty(keyCode)) {
        keyEnum[keyCode] = false;
      }
    });


    // 创建轨道控制器
    const controls = new OrbitControls(camera, renderer.domElement);

    // 方便摆放模型位置
    // controls.addEventListener("change", () => {
    //   console.log(camera.position);
    // })

    // 开启物体的阻尼动画效果 
    controls.enableDamping = true;


    // 添加人物每次移动的向量
    const velocity = new THREE.Vector3(2, 0, 3);
    const reVelocity = new THREE.Vector3(-2, 0, -3);

    // velocity.multiplyScalar(0.01);
    // reVelocity.multiplyScalar(0.01);

    // 此类是 Clock 的替代品，具有不同的 API 设计和行为。目标是避免随着时间的推移 Clock 中变得明显的概念缺陷。
    const timer = new Timer();
    function animate(timestamp) {
      if(keyEnum.W) {
        // player.position.add(velocity);
        player.position.addScaledVector(velocity, 0.01);
      }
      if(keyEnum.S) {
        // player.position.add(reVelocity);
        player.position.addScaledVector(reVelocity, 0.01);
      }


      requestAnimationFrame(animate);
      timer.update(timestamp);
      const delta = timer.getDelta();
      controls.update();
      renderer.render(scene, camera);
    } 
    
    // 添加动画并渲染
    animate();
  </script>
</body>

</html>